package util;
import java.util.ArrayList;
import java.util.Iterator;

import oscP5.*;

public class OscHub {
	OscP5 oscP5;
	int receiveAtPort;
	int sendToPort;
	String host;
	String oscP5event;
	ArrayList listeners;
	
	@SuppressWarnings("deprecation")
	public OscHub() {
		receiveAtPort = 12000;
		sendToPort = 52000;
		host = "127.0.0.1";
		oscP5event = "oscEvent";
		oscP5 = new OscP5(this, host, sendToPort, receiveAtPort, oscP5event);
		listeners = new ArrayList();
	}
	
	@SuppressWarnings("deprecation")
	public OscHub(int sendPort, int recievePort, String host){
		receiveAtPort = recievePort;
		sendToPort = sendPort;
		this.host = host;
		oscP5event = "oscEvent";
		oscP5 = new OscP5(this, host, sendToPort, receiveAtPort, oscP5event);
		listeners = new ArrayList();
	}
	
	public void addListener(OscListener ol){
		listeners.add(ol);
	}

	public void oscEvent(OscIn oscIn) {
		Iterator i = listeners.iterator();
		while(i.hasNext()){
			((OscListener)i.next()).oscEvent(oscIn);
		}
	}

	void sendOsc(String ap, float f) {
		OscMessage oscMsg = oscP5.newMsg(ap);
		oscMsg.add(f);
		oscP5.send(oscMsg);
		
	}

}